import { onLoad } from "@dcloudio/uni-app";
import { ref } from "vue";
import { PackCategoriesRule } from "../../options/base";
import { myPackCategoriesStore, mySchoolStore } from "../../store"
import { goToBack, showModal } from "../../utils/utils";


const schoolStore = mySchoolStore();
const myPackStore = myPackCategoriesStore()

const defaultMPackData : PackCategoriesRule = new Array(4).fill({
	pack_title: `这是默认的头部数据`,
	pack_type: 0,
	pack_desc: `这是默认的描述信息，默认信息，默认信息，默认信息，默认信息，默认信息，默认信息，默认信息`,
	moeny: 0
})

export default {
	setup() {
		let selectIndex = '0';
		const pageType = ref<`select` | `view`>(`select`)
		const checkId = ref('');
		const loading = ref(true);
		const packCategories = ref(defaultMPackData);


		onLoad(async (options) => {
			if (options && options.i) {
				pageType.value = `select`
				selectIndex = options.i
			}
			if (options && options.type) {
				if (options.type == `view`) {
					pageType.value = options.type;
				}
			}
			const list = await myPackStore.getPackCategories(schoolStore.mySchool._id);
			packCategories.value = list;
			loading.value = false;
		})

		const radioChange = (e : { detail : any; }) => {
			const { value } = e.detail;
			checkId.value = value;
		}

		const confimSelect = () => {
			if (pageType.value !== `select`) {
				return false
			}
			if (checkId.value == '') {
				return false
			}
			uni.setStorageSync(`packCategoriesSelect`, JSON.stringify({
				id: checkId.value,
				selectIndex
			}))
			goToBack();
		}

		return {
			pageType,
			checkId,
			packCategories,
			loading,
			myPackStore,
			radioChange,
			confimSelect
		}
	}
}